﻿jQuery(document).ready(function ($) {
    $.postJSON = function (url, data, callback) { $.post(url, data, callback, "json"); };
    var isIE = navigator.userAgent.search("MSIE") > -1;
    function showInquiryTable(data) {
        var tb = $('.InquiryAddToTable');
        tb.empty();
        var header = '<tr class="title"><td class="Number">NO.</td><td class="ModelName">Model Name</td><td>Categories</td><td>Option</td></tr>';
        tb.append(header);

        $('#inquiryCount').html('(' + data.length + ')');

        for (var i = 0; i < data.length; i++) {
            var name = data[i].Name;
            var category = data[i].Category;
            var id = data[i].ID;
            var index = i + 1;

            var row = '<tr id="' + id + '"><td class="Number">' + index
                        + '</td><td class="ModelName">' + name + '</td><td>' + category
                        + '</td><td><input type="button" value="Delete" class="deleteInquiry" rel="' + name + '"/>'
                        + '</td></tr>';

            tb.append(row);
        }

        $('.deleteInquiry').click(function () {
            var modelName = $(this).attr('rel');

            var tb = $('.InquiryAddToTable');
            var url = '/Inquiry/RemovePopUpModel/?modelName=' + modelName;

            $.postJSON(url, null, function (data) {
                showInquiryTable(data);
            });
        });
    }

    if (isIE) {
        $("#inquiryModel").closest("body").append($("#inquiryModel")[0].outerHTML).end().remove();
    }

    $(".ProductDetailButtonAdd").overlay();
    $(".AddAccessories").overlay();

    $('.ProductDetailButtonAdd').click(function () {
        $.postJSON('/Inquiry/PopupModel', { modelName: $(this).attr('modelName'), modelCategory: $(this).attr('modelCategory') }, function (data) {
            showInquiryTable(data);
        });
    });

    $('.SubmitBtn').click(function () {
        window.location.href = "/Inquiry/InquiryModel";
    });

    $('.SubmitBtn2').click(function () {
        window.location.href = "/Inquiry/InquiryModel";
    });

    //tabs        

    var init_tab_index = 0;
    if ($('#modelTabIndex').val() != "") {
        init_tab_index = $('#modelTabIndex').val();
    }

    $("ul.tabsPrd").tabs("div.panes > div.prdtabsContent", 
      { event: 'mouseover', 
        HeaderDiffCss: true, 
        initialIndex: parseInt(init_tab_index), //頁籤一開始顯示的頁數
        onBeforeClick: function (e, i) {
            var thediv = $('.prdtabsContent').eq(i);
            var prdguid = $('#prdguid').val();
            $('#HorPanelLabel').css('height', thediv.eq(0).css('height'));

            try {
                var page_link = "";
                if (navigator.userAgent.search("Firefox") > -1) {  //firfox的toElement是undefined，所以加上此判斷
                    page_link = e.originalTarget.href + '?partial=1';
                }
                else {
                    page_link = e.toElement.href + '?partial=1';
                }
                thediv.load(page_link);
            } catch (e) {
            }
        }
    });



    if (isIE) {

//        $("div [id^='accessoryID-'] ").each(function () {
//            $(this).closest("body").append($(this)[0].outerHTML).end().remove();
//        });


        $("#prdmin").closest("body").append($("#prdmin")[0].outerHTML).end().remove();
    }
    //popup images
    triggers = $(".showprdimg").overlay();
    //$(".accessPopupImg").overlay();


    $("#ProductImageMini img").bind({
        'click': function () {
            var urlMidel = $(this).attr('rel');
            //            var path = '<%=Url.Action(ConstStatic.AN_GETIMG, ConstStatic.CLER_FILEDATA, new { guid ="placeHolder"})%>';
            //            path = path.replace('placeHolder', urlMidel);
            $("span", "#ProductImageMini").removeClass("selected");
            $(this).closest("span").addClass("selected");
            $('#imgMiddlePrd').attr('src', urlMidel);
            //$('#imgMiddlePrd').attr('alt', urlMidel);
            var urlLarge = $(this).attr('large');
            $('#overlayPrdPic').attr('src', urlLarge);
            $('.queueImgs').closest('span').removeClass('selected');
            $('.queueImgs').filter('img[src*=' + $(this).attr('src') + ']').closest('span').addClass('selected');
        }
    });

    $("img","#ProductPopImageMini").bind({ 'click': function () {
        var urlMidel = $(this).attr('rel');
        $('#overlayPrdPic').attr('src', urlMidel);
        $('.queueImgs').closest('span').removeClass('selected');
        $(this).closest('span').addClass('selected');
        //$('#overlayPrdPic').attr('alt', urlMidel);
    }
    });

    $('#overlayPrdPic').click(function () {

        var it = $(this);
        var list = $('.queueImgs');
        var step = 0;

        for (var i = 0; i < list.length; i++) {
            if (list.eq(i).closest('span').hasClass('selected')) {
                if (i == (list.length - 1)) {
                    $("span", "#ProductPopImageMini").removeClass('selected');
                    list.eq(0).closest('span').addClass('selected');
                    $('#overlayPrdPic').attr('src', list.eq(0).attr('rel'));
                    return;
                }
                else {
                    $("span", "#ProductPopImageMini").removeClass('selected');
                    list.eq(i + 1).parents('span:eq(0)').addClass('selected');
                    $('#overlayPrdPic').attr('src', list.eq(i + 1).attr('rel'));
                    return;
                }
            }
        }
    });
});